package com.tencent.pet.service.impl;

import com.tencent.basic.service.IBaiduAiAuditService;
import com.tencent.basic.service.serviceimpl.BaseServiceImpl;
import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.LoginContext;
import com.tencent.org.domain.Emp;
import com.tencent.org.mapper.EmpMapper;
import com.tencent.pet.domain.Pet;
import com.tencent.pet.domain.PetDetail;
import com.tencent.pet.domain.PetOnlineAuditLog;
import com.tencent.pet.mapper.PetDetailMapper;
import com.tencent.pet.mapper.PetMapper;
import com.tencent.pet.mapper.PetOnlineAuditLogMapper;
import com.tencent.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private EmpMapper empMapper;


    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        petDetailMapper.deleteByPetid(id);
    }

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        super.patchDel(ids);
        petDetailMapper.patchDeleteByPetId(ids);
    }

    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        // 1.每一个宠物都要审核，审核name 和 resource
        for (Long id : ids) {
            Pet pet = petMapper.findById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());

            if(boo1 && boo2){
                petMapper.onsale(new Date(), id);
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                // 获取登录信息
                Long logininfo = LoginContext.getLogininfo(request).getId();
                Emp emp = empMapper.findByLogininfoId(logininfo);
                log.setPetId(id);
                log.setState(1);
                log.setAuditId(emp.getId());
                log.setNote("审核成功");
                petOnlineAuditLogMapper.add(log);
                return new AjaxResult();
            }else {
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                // 获取登录信息
                Long logininfo = LoginContext.getLogininfo(request).getId();
                Emp emp = empMapper.findByLogininfoId(logininfo);
                log.setPetId(id);
                log.setState(0);
                log.setAuditId(emp.getId());
                log.setNote("审核失败");
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败");
            }

        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        // 1.改下价时间为当前系统时间，该状态为0
        petMapper.offsale(new Date(), ids);

    }
}
